package com.niit.controller;
import com.niit.pojo.Consumer;
import com.niit.pojo.Admin;
import com.niit.pojo.Window;
import com.niit.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/table_basic")
    public String table_basic(){
        return "table_basic";
    }
    @RequestMapping("/table_data_tables")
    public String table_data_tables(){
        return "table_data_tables";
    }

    @RequestMapping("/login")
    public String login(@RequestParam(value = "error", required = false) String error, Model model) {
        return "login";
    }

    @RequestMapping("/addStudent")
    public String addStudent(Consumer consumer, RedirectAttributes redirectAttributes){
        // 在添加学生之前检查用户名是否已存在
        if (loginService.isUsernameTaken(consumer.getConsumer_id())){
            redirectAttributes.addFlashAttribute("falseMessage", "用户名已经被使用，请选择其他用户名");
            return "redirect:/register"; // 重定向回注册页面并显示错误消息
        }
        int num = loginService.addStudent(consumer);
        if (num > 0) {
            redirectAttributes.addFlashAttribute("successMessage", "注册成功，请登录"); // 添加成功消息
            return "login"; // 重定向到登录页面
        } else {
            redirectAttributes.addFlashAttribute("falseMessage", "注册失败，请重新注册");
            return "register";
        }
    }
    @PostMapping("/authenticate")
    public String authenticate(@RequestParam String username, @RequestParam String password,
                               RedirectAttributes attributes, HttpServletRequest request, Model model) {
        boolean isConsumer = loginService.isConsumer(username, password);
        if (isConsumer) {
            // 在用户表中查找匹配的用户名和密码
            boolean authenticated = loginService.authenticateUser(username, password);
            if (authenticated) {
                // 用户登录成功
                // 将用户信息存储到Session中
                HttpSession session = request.getSession();
                session.setAttribute("user", username);
                Consumer consumer = new Consumer();
                consumer.setConsumer_id(username);
                model.addAttribute("consumer", consumer);
                return "index_customer"; // 重定向到用户界面
            } else {
                attributes.addFlashAttribute("loginError", "用户登录失败，请检查用户名和密码");
                return "redirect:/login?error=true";
            }
        } else {
            // 不是消费者，检查是否是窗口用户
            boolean authenticated = loginService.authenticateWindow(username, password);
            if (authenticated) {
                // 将窗口信息存储到Session中
                HttpSession session = request.getSession();
                session.setAttribute("window", username);
                Window window = new Window();
                window.setWindow_id(username);
                model.addAttribute("window", window);
                return "index_window"; // 重定向到窗口界面
            } else {
                // 不是窗口用户，检查是否是管理员
                authenticated = loginService.authenticateAdmin(username, password);
                if (authenticated) {
                    // 管理员登录成功
                    // 将管理员信息存储到Session中
                    HttpSession session = request.getSession();
                    session.setAttribute("admin", username);
                    Admin admin = new Admin();
                    admin.setAdministrator_id(username);
                    model.addAttribute("admin", admin);
                    return "index_administrator"; // 重定向到管理员界面
                } else {
                    attributes.addFlashAttribute("loginError", "登录失败，请检查用户名和密码");
                    return "redirect:/login?error=true"; // 登录失败后重定向到登录页面并附带错误参数
                }
            }
        }
    }


    @RequestMapping("/index_customer")
    public String index_customer(HttpServletRequest request) {
        // 检查用户是否已经登录
        if (!isUserLoggedIn(request)) {
            return "redirect:/login";
        }
        return "index_consumer";
    }

    private boolean isUserLoggedIn(HttpServletRequest request) {
        // 检查Session中是否存在用户信息
        HttpSession session = request.getSession(false);
        return session != null && session.getAttribute("user") != null;
    }

    @GetMapping("/get")
    @ResponseBody
    public Admin select(Admin admin) {
        return admin;
        
    }

    @PostMapping("/post")
    @ResponseBody
    public Admin select2(Admin admin) {
        return admin;

    }


    @PostMapping("/adddemo")
    @ResponseBody
    public Map<String, String> adddemo(@RequestBody Consumer consumer) {
        Map<String, String> response = new HashMap<>();
        // 在添加学生之前检查用户名是否已存在
        if (loginService.isUsernameTaken(consumer.getConsumer_id())) {
            response.put("message", "用户名已经被使用，请选择其他用户名");
        } else {
            int num = loginService.addStudent(consumer);
            if (num > 0) {
                response.put("message", "注册成功，请登录");
            } else {
                response.put("message", "注册失败，请重新注册");
            }
        }
        return response;
    }



}